# !/usr/bin/python3
# -*- coding:utf-8 -*-
__author__ = u'sangbowei'

import smtplib
from email.mime.text import MIMEText
from email.header import Header

global _allVisaProblemUrlList  # 存放签证 问题url
global _allShipProblemUrlList  # 存放邮轮 问题url
global _sendMailFlag  # 判断是否需要发送邮件，只有初始化完成后才发送邮件

# 第三方 SMTP 服务
mail_host = "smtp.exmail.qq.com"  # 设置服务器
mail_user = "sangbowei@lvmama.com"  # 用户名
mail_pass = "SBWsbw1314"  # 口令

sender = 'sangbowei@lvmama.com'
receivers = ['sangbowei@lvmama.com']  # 接收邮件，可设置为你的QQ邮箱或者其他邮箱


def base_send_email():
    if _sendMailFlag== 'false':
        return ''
    else:
        message = MIMEText(buildEmailContentMsg(), 'html', 'utf-8')
        message['From'] = Header("python监控小柏", 'utf-8')
        message['To'] = Header("出境组", 'utf-8')
        subject = '出境线上异常可售产品汇总'
        message['Subject'] = Header(subject, 'utf-8')

        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
            smtpObj.login(mail_user, mail_pass)
            smtpObj.sendmail(sender, receivers, message.as_string())
            print("邮件发送成功")
        except smtplib.SMTPException:
            print("Error: 无法发送邮件")

def buildEmailContentMsg():
    if _sendMailFlag== 'false':
        return 'no need build message content!'
    else:
        mail_msg = buildSingleHtmlText(_allVisaProblemUrlList, '签证问题链接')
        mail_msg += buildSingleHtmlText(_allShipProblemUrlList, '邮轮问题链接')
        return mail_msg

def buildSingleHtmlText(urlList,title):
    base_mail_msg = """<p style=\"font-size: 20pt\"> """+title+"""</p>"""
    base_mail_msg+= """
        <table style="border-collapse: collapse;border-spacing: 0;"> 
           <tbody>
                <tr> 
                 <td style="border:#000 solid 1px;line-height: 18px;padding: 3px 5px;width:450px;"> URL 链接 </td> 
                 <td style="border:#000 solid 1px;line-height: 18px;padding: 3px 5px;width:150px;"> 耗时(s) </td> 
                 <td style="border:#000 solid 1px;line-height: 18px;padding: 3px 5px;width:180px;"> 耗时(ms) </td> 
                 <td style="border:#000 solid 1px;line-height: 18px;padding: 3px 5px;width:180px;"> 返回状态 </td> 
                </tr> """

    for urlDict in urlList:
        base_mail_msg += """
            <tr> 
                <td style="border:#000 solid 1px;line-height: 18px;padding: 3px 5px;color:#1F497D;width:450px;"><a href='"""+urlDict['url']+ """'>"""+urlDict['url']+"""</a></td> 
                <td style="border:#000 solid 1px;line-height: 18px;padding: 3px 5px;color:#1F497D;width:150px;">"""+urlDict['takeSeconds']+"""</td> 
                <td style="border:#000 solid 1px;line-height: 18px;padding: 3px 5px;color:#1F497D;width:180px;">"""+urlDict['takeMicroSeconds']+"""</td> 
                <td style="border:#000 solid 1px;line-height: 18px;padding: 3px 5px;color:#1F497D;width:180px;">"""+urlDict['statusCode']+"""</td>
            </tr>"""
    base_mail_msg += """</tbody></table>"""
    return base_mail_msg;

def init_problem_url_list(visaUrlList,shipUrlList):
    global _allVisaProblemUrlList
    _allVisaProblemUrlList = visaUrlList
    global  _allShipProblemUrlList
    _allShipProblemUrlList = shipUrlList
    global _sendMailFlag
    _sendMailFlag = 'true'
